package com.jiuyin.dianjing.ui.fragment.community;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.BaseRecyclerAdapter;
import com.jiuyin.dianjing.adapter.SmartViewHolder;
import com.jiuyin.dianjing.api.base.DefaultApiResult;
import com.jiuyin.dianjing.api.enums.ApiEnum;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.event.RefreshEvent;
import com.jiuyin.dianjing.model.UniversityClub;
import com.jiuyin.dianjing.ui.activity.community.CollegeMainActivity;
import com.jiuyin.dianjing.ui.fragment.base.BaseRefreshFragment;
import com.jiuyin.dianjing.view.GridSectionAverageGapItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_PAGE_NUM;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_WORD;
import static com.jiuyin.dianjing.api.constant.ApiConstant.PAGE_FIRST;
import static com.jiuyin.dianjing.api.constant.ApiConstant.PAGE_NUM;

/**
 * Description:
 * File: CollegesListFragment.java
 * Author: Administrator
 * Create: 2020/5/28 17:59
 */
public class OnlineListFragment extends BaseRefreshFragment<UniversityClub> {
    private String mWord;

    public OnlineListFragment() {
        super(PAGE_NUM);
    }

    public static OnlineListFragment newInstance() {
        OnlineListFragment fragment = new OnlineListFragment();
        return fragment;
    }

    public void setWord(String word) {
        this.mWord = word;
    }

    @Override
    public void fetchData() {
        HashMap<String, Object> data = new HashMap<>();
        data.put(KEY_TOKEN, HelperApplication.getToken());
        data.put(KEY_WORD, mWord);
        data.put(KEY_PAGE_NUM, mPageNum);
        JSONObject jsonObject = new JSONObject(data);
        ApiResultCallback requestCallback = new DefaultApiResult() {
            @Override
            public void onSuccess(JsonObject data) {
                if (null != data && data.has("universityClubList")) {
                    List<UniversityClub> clubs = new Gson().fromJson(data.getAsJsonArray("universityClubList"), new TypeToken<List<UniversityClub>>() {
                    }.getType());
                    dealWithSuccessInfo(clubs);
                } else {
                    dealOtherInfo();
                }
            }

            @Override
            public void onError(String msg) {
                dealOtherInfo();
            }

            @Override
            public void onFail(String msg) {
                dealOtherInfo();
            }
        };
        ServerApi.post(ApiEnum.GET_ONLINE_CLUB_LIST, jsonObject, null, progress, d -> addDisposable(d), requestCallback);
    }

    @Override
    protected int setLayoutId() {
        return R.layout.online_list_fragment;
    }

    @Override
    protected void initData() {
        mAdapter = new BaseRecyclerAdapter<UniversityClub>(R.layout.item_college) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, UniversityClub model, int position) {
                ImageView ivHead = holder.itemView.findViewById(R.id.iv_background);
                Glide.with(getContext())
                        .load(model.list_logo)
                        .override(ivHead.getWidth(), ivHead.getHeight())
                        .into(ivHead);
                holder.text(R.id.tv_name, model.name);
            }
        };
        mAdapter.setOnItemClickListener((parent, view, position, id) -> {
            UniversityClub model = mAdapter.getItem(position);
            Bundle bundle = new Bundle();
            bundle.putSerializable("UniversityClub", model);
            goTargetActivity(CollegeMainActivity.class, bundle);
        });
        mRcvPage.setLayoutManager(new GridLayoutManager(getContext(), 2));
        mRcvPage.addItemDecoration(new GridSectionAverageGapItemDecoration(10, 10, 20, 15));
        mRcvPage.setAdapter(mAdapter);
    }

    @Override
    protected void initView() {
    }

    public void refresh() {
        mPageNum = PAGE_FIRST;
        if (isVisible()) {
            fetchData();
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RefreshEvent event) {
        if (event.clazzName.equals(getClass().getSimpleName())) {
            refresh();
        }
    }
}
